package home;

import jdk.internal.util.xml.XMLStreamException;
import jdk.internal.util.xml.impl.XMLWriter;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

public class Home {
    public static void main(String[] args) {
     add();
    }

    public static void select(){
        XMLWriter writer=null;

        try {
            SAXReader reader=new SAXReader();
            Document document = reader.read("ch02/src/home.xml");
            Element root = document.getRootElement();
            List<Element> lsit = root.elements("student");
            for (Element element : lsit) {
                System.out.println("标签名："+element.getName());
                System.out.println("id名："+element.attributeValue("id"));
                Element name = element.element("name");
                Element sourse = element.element("sourse");
                Element score = element.element("score");
                System.out.println("姓名："+name.getText());
                System.out.println("学科："+sourse.getText());
                System.out.println("分数："+score.getText());
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    public static void delete(){
        org.dom4j.io.XMLWriter writer=null;
        try {
            SAXReader reader=new SAXReader();
            Document document = reader.read("ch02/src/home.xml");
            Element root = document.getRootElement();
            List<Element> list = root.elements("student");
            for (Element element : list) {
                String id = element.attributeValue("id");
                if (id.equals("1")){
                    root.remove(element);
                }
            }
            OutputFormat format=OutputFormat.createPrettyPrint();
            format.setEncoding("utf-8");
            writer=new org.dom4j.io.XMLWriter(new FileWriter("ch02/src/home.xml"),format);
            writer.write(root);
            writer.flush();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void update(){
        org.dom4j.io.XMLWriter writer=null;
        try {
            SAXReader reader=new SAXReader();
            Document document = reader.read("ch02/src/home.xml");
            Element root = document.getRootElement();
            List<Element> list = root.elements("student");
            for (Element element : list) {
                String id = element.attributeValue("id");
                if (id.equals("2")){
                    Element score = element.element("score");
                    score.setText("60");
                }
            }
            OutputFormat format=OutputFormat.createPrettyPrint();
            format.setEncoding("utf-8");
            writer=new org.dom4j.io.XMLWriter(new FileWriter("ch02/src/home.xml"),format);
            writer.write(root);
            writer.flush();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void add(){
        org.dom4j.io.XMLWriter writer=null;
        try {
            SAXReader reader=new SAXReader();
            Document document = reader.read("ch02/src/home.xml");
            Element root = document.getRootElement();
            Element student = root.addElement("student");
            Element name = student.addElement("name");
            name.setText("王武");
            Element sourse = student.addElement("sourse");
            sourse.setText("语文");
            Element score = student.addElement("score");
            score.setText("66");
            student.addAttribute("id","3");

            OutputFormat format=OutputFormat.createPrettyPrint();
            format.setEncoding("utf-8");
            writer=new org.dom4j.io.XMLWriter(new FileWriter("ch02/src/home.xml"),format);
            writer.write(root);
            writer.flush();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
